package com.panshi.service.impl;

import com.panshi.mapper.IQuestionMapper;
import com.panshi.mapper.IReplyAuditMapper;
import com.panshi.mapper.IReplyMapper;
import com.panshi.service.IReplyAuditService;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.Reply;
import com.panshi.vo.qa.ReplyAudit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ReplyAuditService implements IReplyAuditService {

    @Autowired
    IReplyAuditMapper replyAuditMapper;

    @Autowired
    IReplyMapper replyMapper;

    @Autowired
    IQuestionMapper questionMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean addReplyAudit(ReplyAudit replyAudit) {
        try {
            replyAuditMapper.addReplyAudit(replyAudit);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public Pages<ReplyAudit> inquirePagesReplyAudit(Pages pages) {
        List<ReplyAudit> list = replyAuditMapper.inquirePagesReplyAudit(pages);
        int sum = replyAuditMapper.inquireReplyAuditSum(pages);
        Pages<ReplyAudit> p = new Pages<>();
        p.setList(list);
        p.setSum(sum);
        return p;
    }

    @Override
    public Integer inquireReplyAuditSum(Pages pages) {
        int sum = replyAuditMapper.inquireReplyAuditSum(pages);
        return sum;
    }

    @Override
    @Cacheable(value = "replyAudit:inquireById",key = "#id")
    public ReplyAudit inquireById(int id) {
        ReplyAudit replyAudit = replyAuditMapper.inquireById(id);
        return replyAudit;
    }

    @Override
    @CacheEvict(value = "replyAudit:inquireById",key = "#p0.r_id")
    public boolean updateReplyAudit(ReplyAudit replyAudit) {
        try {
            replyAuditMapper.updateReplyAudit(replyAudit);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    @CacheEvict(value = "replyAudit:inquireById",key = "#id")
    public boolean deleteReplyAudit(int id) {
        try {
            replyAuditMapper.deleteReplyAudit(id);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    @CacheEvict(value = "replyAudit:inquireById",key = "#p0.r_id")//删除reids中回答审核
    public boolean updateState(ReplyAudit replyAudit) {
        try {
            replyAuditMapper.updateState(replyAudit);
            //a_state == 2 说明是审核通过的直接添加到 回答表
            if (replyAudit.getRState() == 2){

                Integer rid = replyAudit.getRId();
                ReplyAudit ra = replyAuditMapper.inquireById(rid);

                //创建回答
                Reply reply = new Reply();

                //所属用户id
                int rUserId = ra.getRUserId();
                reply.setRUserId(rUserId);
                //所属问题id
                int rQuestionId = ra.getRQuestionId();
                reply.setRQuestionId(rQuestionId);
                //回答内容
                String rContent = ra.getRContent();
                reply.setRContent(rContent);
                //回答摘要()
                String rAbstract = null;
                if(rContent.length() > 80){
                    rAbstract = rContent.substring(0,80);
                }else{
                    rAbstract = rContent;
                }
                reply.setRAbstract(rAbstract);
                //回答图片url
                String rImgUrl = ra.getRImgUrl();
                reply.setRImgUrl(rImgUrl);
                //回答时间
                String rDate = ra.getRDate();
                reply.setRDate(rDate);

                //添加回答
                replyMapper.addReply(reply);

                //问题回答总数加一
                questionMapper.commentSumAnd1(rQuestionId);
            }
            return true;
        }catch(Exception e){
            return false;
        }
    }
}
